﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Forms;
using TIBCO.Rendezvous;

namespace TibcoWinform
{
    public partial class RendezvousSender : UserControl
    {
        TIBCO.Rendezvous.NetTransport transport;
        System.Timers.Timer timer;
        TIBCO.Rendezvous.Message msg;
        Double interval;
        int msgcount;
        CustomerDataStruct data;
        const string filename = "CustomerDataConfig.json";
        public RendezvousSender()
        {
            InitializeComponent();
        }
        public void Init()
        {
            TIBCO.Rendezvous.Environment.Open();
            msg = new TIBCO.Rendezvous.Message();
            Enable_Create_Transport();
            Disable_Destroy_Transport();
            Disable_Send_Many();
            Btn_SendOne.Enabled = false;
            transport = null;
            defaultRaBtn.Checked = true;
            LoadData();
        }
        public void Destory()
        {
            TIBCO.Rendezvous.Environment.Close();
        }
        private void LoadData()
        {
            try
            {
                //data = SerializableHelper.Read<CustomerDataStruct>(filename);
                data = SerializableHelper.ReadFromXML<CustomerDataStruct>();
            }
            catch
            {
                data = new CustomerDataStruct();
            }
            propertyGrid1.SelectedObject = data;
            propertyGrid1.ExpandAllGridItems();
        }
        private void SaveData()
        {
            try
            {
                SerializableHelper.Save2(data, filename);
                SerializableHelper.SaveToXML(data);
            }
            catch
            {
            }
        }
        private void Disable_Send_Many()
        {
            Txt_Count.Enabled = false;
            Lbl_Count.Enabled = false;
            Txt_Interval.Enabled = false;
            Lbl_Interval.Enabled = false;
            Btn_SendMany.Enabled = false;
        }

        private void Disable_Destroy_Transport()
        {
            Btn_DestroyXport.Enabled = false;
        }

        private void Enable_Create_Transport()
        {
            Txt_Service.Enabled = true;
            Txt_Network.Enabled = true;
            Txt_Daemon.Enabled = true;
            Txt_Subject.Enabled = true;
            Lbl_Service.Enabled = true;
            Lbl_Network.Enabled = true;
            Lbl_Daemon.Enabled = true;
            Lbl_Subject.Enabled = true;

            Disable_Send_Many();
            Btn_SendOne.Enabled = false;

            Btn_CreateXport.Enabled = true;
        }

        private void Btn_CreateXport_Click(object sender, EventArgs e)
        {
            try
            {
                transport = new TIBCO.Rendezvous.NetTransport(Txt_Service.Text, Txt_Network.Text, Txt_Daemon.Text);
                Disable_Create_Transport();
                Enable_Destroy_Transport();
                Enable_Send_Many();
                Btn_SendOne.Enabled = true;
                Txt_MessageSentCount.Text = "0";
            }
            catch (TIBCO.Rendezvous.RendezvousException exception)
            {
                MessageBox.Show("Unable to Create Transport,  Reason :  " + exception.Message);
            }
        }

        private void Enable_Send_Many()
        {
            ControlHelper.InvokeIfNeeded(this, new Action(() =>
           {
               Txt_Count.Enabled = true;
               Lbl_Count.Enabled = true;
               Txt_Interval.Enabled = true;
               Lbl_Interval.Enabled = true;
               Btn_SendMany.Enabled = true;
           }));
        }

        private void Enable_Destroy_Transport()
        {
            Btn_DestroyXport.Enabled = true;
        }

        private void Disable_Create_Transport()
        {
            Txt_Service.Enabled = false;
            Txt_Network.Enabled = false;
            Txt_Daemon.Enabled = false;
            Lbl_Service.Enabled = false;
            Lbl_Network.Enabled = false;
            Lbl_Daemon.Enabled = false;
            Btn_CreateXport.Enabled = false;
        }

        private void Btn_DestroyXport_Click(object sender, EventArgs e)
        {
            if (transport != null)
                transport.Destroy();
            transport = null;
            msgcount = 0;
            Disable_Destroy_Transport();
            Enable_Create_Transport();
        }

        private void Btn_Exit_Click(object sender, EventArgs e)
        {
            TIBCO.Rendezvous.Environment.Close();
        }

        private void Btn_SendMany_Click(object sender, EventArgs e)
        {
            msgcount = int.Parse(Txt_Count.Text);
            interval = double.Parse(Txt_Interval.Text);
            if (msgcount > 0)
            {
                Btn_SendOne_Click(this, e);
                msgcount = msgcount - 1;
            }
            if (msgcount > 0)
            {
                timer = new System.Timers.Timer(interval);
                timer.Elapsed += OnTimer;
                timer.Enabled = true;
                Disable_Send_Many();
            }
        }

        private void OnTimer(object sender, ElapsedEventArgs e)
        {
            if (msgcount <= 0)
                timer.Enabled = false;
            Enable_Send_Many();

            msgcount = msgcount - 1;
            Btn_SendOne_Click(this, e);
        }

        private void Btn_SendOne_Click(object sender, EventArgs e)
        {
            msg.Reset();
            if (!Grp_Box3.Enabled)
            {
                var defectinfomsg = new TIBCO.Rendezvous.Message();
                defectinfomsg.AddField("LOTID", data.BODY.DEFECT_INFO.LOTID);
                defectinfomsg.AddField("GLASSID", data.BODY.DEFECT_INFO.GLASSID);
                defectinfomsg.AddField("DEFECTNO", data.BODY.DEFECT_INFO.DEFECTNO);
                defectinfomsg.AddField("DEFECTCODE", data.BODY.DEFECT_INFO.DEFECTCODE);
                defectinfomsg.AddField("DEFECTPOSITION", data.BODY.DEFECT_INFO.DEFECTPOSITION);
                defectinfomsg.AddField("MAJORDEFECT", data.BODY.DEFECT_INFO.MAJORDEFECT);
                defectinfomsg.AddField("FLAG", data.BODY.DEFECT_INFO.FLAG);
                defectinfomsg.AddField("DESCRIPTION", data.BODY.DEFECT_INFO.DESCRIPTION);
                var bodymsg = new TIBCO.Rendezvous.Message();
                bodymsg.AddField("COMMENT", data.BODY.COMMENT);
                bodymsg.AddField("EQPID", data.BODY.EQPID);
                bodymsg.AddField("FINALGRADE", data.BODY.FINALGRADE);
                bodymsg.AddField("LINEID", data.BODY.LINEID);
                bodymsg.AddField("LOTID", data.BODY.LOTID);
                bodymsg.AddField("NEXTOPERID", data.BODY.NEXTOPERID);
                bodymsg.AddField("QTY", data.BODY.QTY);
                bodymsg.AddField("RWFLAG", data.BODY.RWFLAG);
                bodymsg.AddField("USERID", data.BODY.USERID);
                bodymsg.AddField("DEFECT_INFO", defectinfomsg);
                msg.AddField("NAME", data.NAME);
                msg.AddField("ID", data.ID);
                msg.AddField("BODY", bodymsg);
            }
            else if (double.TryParse(Txt_FieldValue.Text, out double field))//是否数字
            {
                if (Txt_FieldValue.Text.IndexOf('.') == 1)
                    msg.AddField(Txt_FieldName.Text, Math.Round(field));
                else
                    msg.AddField(Txt_FieldName.Text, double.Parse(Txt_FieldValue.Text));
            }
            else if (DateTime.TryParse(Txt_FieldValue.Text, out DateTime dt))//是否日期
            {
                msg.AddField(Txt_FieldName.Text, dt);
            }
            else if (string.IsNullOrEmpty(Txt_FieldValue.Text))
            {
                if (string.IsNullOrEmpty(Txt_FieldName.Text))
                    msg.AddField(Txt_FieldName.Text, "");
            }
            else
                msg.AddField(Txt_FieldName.Text, Txt_FieldValue.Text);

            try
            {
                ControlHelper.InvokeIfNeeded(this, new Action(() =>
              {
                  msg.SendSubject = Txt_Subject.Text;
                  transport.Send(msg);
                  Txt_MessageSentCount.Text = (int.Parse(Txt_MessageSentCount.Text) + 1).ToString();
              }));

            }
            catch (RendezvousException ex)
            {
                MessageBox.Show("Unable to Send Message,  Reason :  " + ex.Message);
            }
        }

        private void SaveBtn_Click(object sender, EventArgs e)
        {
            SaveData();
        }

        private void customerRaBtn_CheckedChanged(object sender, EventArgs e)
        {
            if (defaultRaBtn.Checked)
            {
                splitContainer1.Panel2Collapsed = true;
                Grp_Box3.Enabled = true;
            }
            else
            {
                splitContainer1.Panel2Collapsed = false;
                Grp_Box3.Enabled = false;
            }
        }
    }
}
